package com.tjian.fafa.user.base.mainBody;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.slidingmenu.lib.SlidingMenu;
import com.tjian.fafa.user.R;
import com.tjian.fafa.user.module.home.bean.NewsClassify;
import com.tjian.fafa.user.module.home.fragment.BicycleFragment;
import com.tjian.fafa.user.module.home.fragment.Develop_test_page_home_fragment;
import com.tjian.fafa.user.module.home.fragment.NewsFragmentPagerAdapter;
import com.tjian.fafa.user.module.home.tool.BaseTools;
import com.tjian.fafa.user.module.home.tool.Constants_home;
import com.tjian.fafa.user.module.home.view.ColumnHorizontalScrollView;

import java.util.ArrayList;

@SuppressWarnings("deprecation")
public class MainTabHomePage_fragment extends ActivityGroup
{
    private static MainTabHomePage_fragment mainTabHomePage_instance;
    private View viewOfPage;
    private Context context_class;

    /**
     * 自定义HorizontalScrollView
     */
    private ColumnHorizontalScrollView mColumnHorizontalScrollView;
    LinearLayout mRadioGroup_content;
    LinearLayout ll_more_columns;
    RelativeLayout rl_column;
    private ViewPager mViewPager;
    /**
     * 新闻分类列表
     */
    private ArrayList<NewsClassify> newsClassify = new ArrayList<NewsClassify>();
    /**
     * 当前选中的栏目
     */
    private int columnSelectIndex = 0;
    /**
     * 左阴影部分
     */
    public ImageView shade_left;
    /**
     * 右阴影部分
     */
    public ImageView shade_right;
    /**
     * 屏幕宽度
     */
    private int mScreenWidth = 0;
    /**
     * Item宽度
     */
    private int mItemWidth = 0;
    private ArrayList<Fragment> fragments = new ArrayList<Fragment>();
    protected SlidingMenu side_drawer;
    protected Activity activity;

    public static MainTabHomePage_fragment getInstance()
    {
        if (mainTabHomePage_instance == null)
        {
            mainTabHomePage_instance = new MainTabHomePage_fragment();
        }
        return mainTabHomePage_instance;
    }

    public void onInit(View view, Context context)
    {
        this.viewOfPage = view;
        context_class = context;
        this.activity = (Activity) context_class;



    }

    public void onCreate()
    {






        /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            this.activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }*/



        mScreenWidth = BaseTools.getWindowsWidth(this.activity);
        mItemWidth = mScreenWidth / 4;// 一个Item宽度为屏幕的1/4
        initView();
    }

    /**
     * 初始化layout控件
     */
    private void initView()
    {
        mColumnHorizontalScrollView = (ColumnHorizontalScrollView) this.viewOfPage.findViewById(R.id.mColumnHorizontalScrollView);
        mRadioGroup_content = (LinearLayout) this.viewOfPage.findViewById(R.id.mRadioGroup_content);
        rl_column = (RelativeLayout) this.viewOfPage.findViewById(R.id.rl_column);
        mViewPager = (ViewPager) this.viewOfPage.findViewById(R.id.mViewPager);
        shade_left = (ImageView) this.viewOfPage.findViewById(R.id.shade_left);
        shade_right = (ImageView) this.viewOfPage.findViewById(R.id.shade_right);
        setChangelView();
    }

    /**
     * 当栏目项发生变化时候调用
     */
    private void setChangelView()
    {
        initColumnData();
        initTabColumn();
        initFragment();
    }

    /**
     * 获取Column栏目 数据
     */
    private void initColumnData()
    {
        newsClassify = Constants_home.getData();
    }

    /**
     * 初始化Column栏目项
     */
    private void initTabColumn()
    {
        mRadioGroup_content.removeAllViews();
        int count = newsClassify.size();
        mColumnHorizontalScrollView.setParam(this, mScreenWidth, mRadioGroup_content, shade_left, shade_right, ll_more_columns, rl_column);
        for (int i = 0; i < count; i++)
        {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mItemWidth, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.leftMargin = 5;
            params.rightMargin = 5;
            //			TextView localTextView = (TextView) mInflater.inflate(R.layout.column_radio_item, null);
            TextView columnTextView = new TextView(context_class);
            columnTextView.setTextAppearance(context_class, R.style.top_category_scroll_view_item_text);
            //			localTextView.setBackground(getResources().getDrawable(R.drawable.top_category_scroll_text_view_bg));
            columnTextView.setBackgroundResource(R.drawable.radio_buttong_bg);
            columnTextView.setGravity(Gravity.CENTER);
            columnTextView.setPadding(5, 5, 5, 5);
            columnTextView.setId(i);
            columnTextView.setText(newsClassify.get(i).getTitle());
            columnTextView.setTextColor(this.context_class.getResources().getColorStateList(R.color.top_category_scroll_text_color_day));
            if (columnSelectIndex == i)
            {
                columnTextView.setSelected(true);
            }
            columnTextView.setOnClickListener(new View.OnClickListener()
            {

                @Override
                public void onClick(View v)
                {
                    for (int i = 0; i < mRadioGroup_content.getChildCount(); i++)
                    {
                        View localView = mRadioGroup_content.getChildAt(i);
                        if (localView != v)
                            localView.setSelected(false);
                        else
                        {
                            localView.setSelected(true);
                            mViewPager.setCurrentItem(i);
                        }
                    }
                    Toast.makeText(context_class.getApplicationContext(), newsClassify.get(v.getId()).getTitle(), Toast.LENGTH_SHORT).show();
                }
            });
            mRadioGroup_content.addView(columnTextView, i, params);
        }
    }

    /**
     * 选择的Column里面的Tab
     */
    private void selectTab(int tab_postion)
    {
        columnSelectIndex = tab_postion;
        for (int i = 0; i < mRadioGroup_content.getChildCount(); i++)
        {
            View checkView = mRadioGroup_content.getChildAt(tab_postion);
            int k = checkView.getMeasuredWidth();
            int l = checkView.getLeft();
            int i2 = l + k / 2 - mScreenWidth / 2;
            mColumnHorizontalScrollView.smoothScrollTo(i2, 0);
        }
        //判断是否选中
        for (int j = 0; j < mRadioGroup_content.getChildCount(); j++)
        {
            View checkView = mRadioGroup_content.getChildAt(j);
            boolean ischeck;
            if (j == tab_postion)
            {
                ischeck = true;
            }
            else
            {
                ischeck = false;
            }
            checkView.setSelected(ischeck);
        }
    }

    /**
     * 初始化Fragment
     */
    private void initFragment()
    {
        int count = newsClassify.size();
        for (int i = 0; i < count; i++)
        {

        }

        //快递车
        Bundle newsBundle = new Bundle();
        newsBundle.putString("text", "");
        Develop_test_page_home_fragment develop_test_page_home_fragment = new Develop_test_page_home_fragment();
        develop_test_page_home_fragment.setArguments(newsBundle);
        //单车快递
        Bundle bicycleBundle = new Bundle();
        bicycleBundle.putString("text", "");
        BicycleFragment bicycleFragment = new BicycleFragment();
        bicycleFragment.setArguments(bicycleBundle);

        //人力快递
        Bundle manpowerBundle = new Bundle();
        manpowerBundle.putString("text", "");
        BicycleFragment manpowerFragment = new BicycleFragment();
        manpowerFragment.setArguments(manpowerBundle);
        //大货车
        Bundle truckBundle = new Bundle();
        truckBundle.putString("text", "");
        BicycleFragment truckFragment = new BicycleFragment();
        truckFragment.setArguments(truckBundle);

        fragments.add(develop_test_page_home_fragment);
        fragments.add(bicycleFragment);

        //fragments.add(manpowerFragment);
        //fragments.add(truckFragment);



        NewsFragmentPagerAdapter newsFragmentPagerAdapter = new NewsFragmentPagerAdapter(((MainBodyActivity) context_class).getSupportFragmentManager(), fragments);
        mViewPager.setAdapter(newsFragmentPagerAdapter);



        mViewPager.setOffscreenPageLimit(0);//里面额参数几时你要缓存页面的个数，建议不要设置太大！
        mViewPager.setOnPageChangeListener(pageListener);
    }

    /**
     * ViewPager切换监听方法
     */
    public ViewPager.OnPageChangeListener pageListener = new ViewPager.OnPageChangeListener()
    {

        @Override
        public void onPageScrollStateChanged(int arg0)
        {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2)
        {
        }

        @Override
        public void onPageSelected(int position)
        {
            mViewPager.setCurrentItem(position);
            selectTab(position);
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.home_menu_main, menu);
        return true;
    }

    private long mExitTime;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            if (side_drawer.isMenuShowing() || side_drawer.isSecondaryMenuShowing())
            {
                side_drawer.showContent();
            }
            else
            {
                if ((System.currentTimeMillis() - mExitTime) > 2000)
                {
                    Toast.makeText(this, "在按一次退出",
                            Toast.LENGTH_SHORT).show();
                    mExitTime = System.currentTimeMillis();
                }
                else
                {
                    finish();
                }
            }
            return true;
        }
        //拦截MENU按钮点击事件，让他无任何操作
        if (keyCode == KeyEvent.KEYCODE_MENU)
        {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
    }

    public Context getmContext()
    {
        return context_class;
    }

    public void setmContext(Context context_class)
    {
        this.context_class = context_class;
    }

}
